<?php

class Model_User extends PhalApi_Model_NotORM
{
    /* 用户全部信息 */
    public function getBaseInfo($uid)
    {
        $info = DI()->notorm->users
            ->select("id,user_nicename,avatar,avatar_thumb,sex,signature,coin,votes,consumption,votestotal,province,city,birthday")
            ->where('id=?  and user_type="2"', $uid)
            ->fetchOne();
        if ($info) {
            $info['avatar'] = get_upload_path($info['avatar']);
            $info['avatar_thumb'] = get_upload_path($info['avatar_thumb']);
            $info['level'] = getLevel($info['consumption']);
            $info['level_anchor'] = getLevelAnchor($info['votestotal']);
            $info['lives'] = getLives($uid);
            $info['follows'] = getFollows($uid);
            $info['fans'] = getFans($uid);

            $info['vip'] = getUserVip($uid);
            $info['liang'] = getUserLiang($uid);
        }


        return $info;
    }

    /* 守护列表   礼物排行榜 */

    public function getGift($uid, $type, $p)
    {
        if ($p < 1) {
            $p = 1;
        }
        $pnum = 50;
        $start = ($p - 1) * $pnum;

        switch ($type) {
            case 'day':
                //获取今天开始结束时间
                $dayStart = strtotime(date("Y-m-d"));
                $dayEnd = strtotime(date("Y-m-d 23:59:59"));
                $where = " addtime >={$dayStart} and addtime<={$dayEnd} and ";

                break;

            case 'week':
                $w = date('w');
                //获取本周开始日期，如果$w是0，则表示周日，减去 6 天
                $first = 1;
                //周一
                $week = date('Y-m-d H:i:s', strtotime(date("Ymd") . "-" . ($w ? $w - $first : 6) . ' days'));
                $week_start = strtotime(date("Ymd") . "-" . ($w ? $w - $first : 6) . ' days');

                //本周结束日期
                //周天
                $week_end = strtotime("{$week} +1 week") - 1;

                $where = " addtime >={$week_start} and addtime<={$week_end} and ";

                break;

            case 'month':
                //本月第一天
                $month = date('Y-m-d', strtotime(date("Ym") . '01'));
                $month_start = strtotime(date("Ym") . '01');

                //本月最后一天
                $month_end = strtotime("{$month} +1 month") - 1;

                $where = " addtime >={$month_start} and addtime<={$month_end} and ";

                break;

            case 'total':
                $where = " ";
                break;

            default:
                //获取今天开始结束时间
                $dayStart = strtotime(date("Y-m-d"));
                $dayEnd = strtotime(date("Y-m-d 23:59:59"));
                $where = " addtime >={$dayStart} and addtime<={$dayEnd} and ";
                break;
        }

        $where .= " type='expend' and action in ('sendgift','sendbarrage') and touid=" . $uid;

        $result = DI()->notorm->users_coinrecord
            ->select('sum(totalcoin) as totalcoin,touid as uid')
            ->where($where)
            ->group('uid')
            ->order('totalcoin desc')
            ->limit($start, $pnum)
            ->fetchAll();

        foreach ($result as $k => $v) {
            $userinfo = getUserInfo($v['uid']);
            $v['avatar'] = $userinfo['avatar'];
            $v['avatar_thumb'] = $userinfo['avatar_thumb'];
            $v['user_nicename'] = $userinfo['user_nicename'];
            $v['sex'] = $userinfo['sex'];
            $v['level'] = $userinfo['level'];
            $v['level_anchor'] = $userinfo['level_anchor'];

            $v['isAttention'] = isAttention($uid, $v['uid']);//判断当前用户是否关注了该主播

            $result[$k] = $v;
        }

    }

    /* 判断昵称是否重复 */
    public function checkName($uid, $name)
    {
        $isexist = DI()->notorm->users
            ->select('id')
            ->where('id!=? and user_nicename=?', $uid, $name)
            ->fetchOne();
        if ($isexist) {
            return 0;
        } else {
            return 1;
        }
    }

    /* 修改信息 */
    public function userUpdate($uid, $fields)
    {
        /* 清除缓存 */
        delCache("userinfo_" . $uid);

        if (!$fields) {
            return false;
        }

        return DI()->notorm->users
            ->where('id=?', $uid)
            ->update($fields);
    }

    /* 修改密码 */
    public function updatePass($uid, $oldpass, $pass)
    {
        $userinfo = DI()->notorm->users
            ->select("user_pass")
            ->where('id=?', $uid)
            ->fetchOne();
        $oldpass = setPass($oldpass);
        if ($userinfo['user_pass'] != $oldpass) {
            return 1003;
        }
        $newpass = setPass($pass);
        return DI()->notorm->users
            ->where('id=?', $uid)
            ->update(array("user_pass" => $newpass));
    }

    /* 我的钻石 */
    public function getBalance($uid)
    {
        return DI()->notorm->users
            ->select("coin")
            ->where('id=?', $uid)
            ->fetchOne();
    }

    /* 充值规则 */
    public function getChargeRules()
    {

        $rules = DI()->notorm->charge_rules
            ->select('id,coin,money,money_ios,product_id,give')
            ->order('orderno asc')
            ->fetchAll();

        return $rules;
    }

    /* 我的收益 */
    public function getProfit($uid)
    {
        $info = DI()->notorm->users
            ->select("votes,votestotal")
            ->where('id=?', $uid)
            ->fetchOne();

        $config = getConfigPri();

        //提现比例
        $cash_rate = $config['cash_rate'];
        $cash_start = $config['cash_start'];
        $cash_end = $config['cash_end'];
        $cash_max_times = $config['cash_max_times'];
        //剩余票数
        $votes = $info['votes'];

        //总可提现数
        $total = (string)floor($votes / $cash_rate);

        if ($cash_max_times) {
            //$tips='每月'.$cash_start.'-'.$cash_end.'号可进行提现申请，收益将在'.($cash_end+1).'-'.($cash_end+5).'号统一发放，每月只可提现'.$cash_max_times.'次';
            $tips = '每月' . $cash_start . '-' . $cash_end . '号可进行提现申请，每月只可提现' . $cash_max_times . '次';
        } else {
            //$tips='每月'.$cash_start.'-'.$cash_end.'号可进行提现申请，收益将在'.($cash_end+1).'-'.($cash_end+5).'号统一发放';
            $tips = '每月' . $cash_start . '-' . $cash_end . '号可进行提现申请';
        }

        $rs = array(
            "votes" => $votes,
            "votestotal" => $info['votestotal'],
            "total" => $total,
            "cash_rate" => $cash_rate,
            "tips" => $tips,
        );
        return $rs;
    }

    /* 提现  */
    public function setCash($data)
    {

        $nowtime = time();

        $uid = $data['uid'];
        $accountid = $data['accountid'];
        $cashvote = $data['cashvote'];

        $config = getConfigPri();
        $cash_start = $config['cash_start'];
        $cash_end = $config['cash_end'];
        $cash_max_times = $config['cash_max_times'];

        $day = (int)date("d", $nowtime);

        if ($day < $cash_start || $day > $cash_end) {
            return 1005;
        }

        //本月第一天
        $month = date('Y-m-d', strtotime(date("Ym", $nowtime) . '01'));
        $month_start = strtotime(date("Ym", $nowtime) . '01');

        //本月最后一天
        $month_end = strtotime("{$month} +1 month");

        if ($cash_max_times) {
            $isexist = DI()->notorm->users_cashrecord
                ->where('uid=? and addtime > ? and addtime < ?', $uid, $month_start, $month_end)
                ->count();
            if ($isexist >= $cash_max_times) {
                return 1006;
            }
        }

        $isrz = DI()->notorm->users_auth
            ->select("status")
            ->where('uid=?', $uid)
            ->fetchOne();
        if (!$isrz || $isrz['status'] != 1) {
            return 1003;
        }

        /* 钱包信息 */
        $accountinfo = DI()->notorm->users_cash_account
            ->select("*")
            ->where('id=?', $accountid)
            ->fetchOne();
        if (!$accountinfo) {
            return 1006;
        }


        //提现比例
        $cash_rate = $config['cash_rate'];
        /* 最低额度 */
        $cash_min = $config['cash_min'];

        //提现钱数
        $money = floor($cashvote / $cash_rate);

        if ($money < $cash_min) {
            return 1004;
        }

        $cashvotes = $money * $cash_rate;


        $ifok = DI()->notorm->users
            ->where('id = ? and votes>=?', $uid, $cashvotes)
            ->update(array('votes' => new NotORM_Literal("votes - {$cashvotes}")));
        if (!$ifok) {
            return 1001;
        }


        $data = array(
            "uid" => $uid,
            "money" => $money,
            "votes" => $cashvotes,
            "orderno" => $uid . '_' . $nowtime . rand(100, 999),
            "status" => 0,
            "addtime" => $nowtime,
            "uptime" => $nowtime,
            "type" => $accountinfo['type'],
            "account_bank" => $accountinfo['account_bank'],
            "account" => $accountinfo['account'],
            "name" => $accountinfo['name'],
        );

        $rs = DI()->notorm->users_cashrecord->insert($data);
        if (!$rs) {
            return 1002;
        }


        return $rs;
    }

    /* 关注 */
    public function setAttent($uid, $touid)
    {
        $isexist = DI()->notorm->users_attention
            ->select("*")
            ->where('uid=? and touid=?', $uid, $touid)
            ->fetchOne();
        if ($isexist) {
            DI()->notorm->users_attention
                ->where('uid=? and touid=?', $uid, $touid)
                ->delete();
            return 0;
        } else {
            DI()->notorm->users_black
                ->where('uid=? and touid=?', $uid, $touid)
                ->delete();
            DI()->notorm->users_attention
                ->insert(array("uid" => $uid, "touid" => $touid));
            return 1;
        }
    }

    /* 拉黑 */
    public function setBlack($uid, $touid)
    {
        $isexist = DI()->notorm->users_black
            ->select("*")
            ->where('uid=? and touid=?', $uid, $touid)
            ->fetchOne();
        if ($isexist) {
            DI()->notorm->users_black
                ->where('uid=? and touid=?', $uid, $touid)
                ->delete();
            return 0;
        } else {
            DI()->notorm->users_attention
                ->where('uid=? and touid=?', $uid, $touid)
                ->delete();
            DI()->notorm->users_black
                ->insert(array("uid" => $uid, "touid" => $touid));

            return 1;
        }
    }

    /* 关注列表 */
    public function getFollowsList($uid, $touid, $p)
    {
        if ($p < 1) {
            $p = 1;
        }
        $pnum = 50;
        $start = ($p - 1) * $pnum;
        $touids = DI()->notorm->users_attention
            ->select("touid")
            ->where('uid=?', $touid)
            ->limit($start, $pnum)
            ->fetchAll();
        foreach ($touids as $k => $v) {
            $userinfo = getUserInfo($v['touid']);
            if ($userinfo) {
                if ($uid == $touid) {
                    $isattent = '1';
                } else {
                    $isattent = isAttention($uid, $v['touid']);
                }
                $userinfo['isattention'] = $isattent;
                $touids[$k] = $userinfo;
            } else {
                DI()->notorm->users_attention->where('uid=? or touid=?', $v['touid'], $v['touid'])->delete();
                unset($touids[$k]);
            }
        }
        $touids = array_values($touids);
        return $touids;
    }

    /* 粉丝列表 */
    public function getFansList($uid, $touid, $p)
    {
        if ($p < 1) {
            $p = 1;
        }
        $pnum = 50;
        $start = ($p - 1) * $pnum;
        $touids = DI()->notorm->users_attention
            ->select("uid")
            ->where('touid=?', $touid)
            ->limit($start, $pnum)
            ->fetchAll();
        foreach ($touids as $k => $v) {
            $userinfo = getUserInfo($v['uid']);
            if ($userinfo) {
                $userinfo['isattention'] = isAttention($uid, $v['uid']);
                $touids[$k] = $userinfo;
            } else {
                DI()->notorm->users_attention->where('uid=? or touid=?', $v['uid'], $v['uid'])->delete();
                unset($touids[$k]);
            }

        }
        $touids = array_values($touids);
        return $touids;
    }

    /* 黑名单列表 */
    public function getBlackList($uid, $touid, $p)
    {
        if ($p < 1) {
            $p = 1;
        }
        $pnum = 50;
        $start = ($p - 1) * $pnum;
        $touids = DI()->notorm->users_black
            ->select("touid")
            ->where('uid=?', $touid)
            ->limit($start, $pnum)
            ->fetchAll();
        foreach ($touids as $k => $v) {
            $userinfo = getUserInfo($v['touid']);
            if ($userinfo) {
                $touids[$k] = $userinfo;
            } else {
                DI()->notorm->users_black->where('uid=? or touid=?', $v['touid'], $v['touid'])->delete();
                unset($touids[$k]);
            }
        }
        $touids = array_values($touids);
        return $touids;
    }

    /* 直播记录 */
    public function getLiverecord($touid, $p)
    {
        if ($p < 1) {
            $p = 1;
        }
        $pnum = 50;
        $start = ($p - 1) * $pnum;
        $record = DI()->notorm->users_liverecord
            ->select("id,uid,nums,starttime,endtime,title,city")
            ->where('uid=?', $touid)
            ->order("id desc")
            ->limit($start, $pnum)
            ->fetchAll();
        foreach ($record as $k => $v) {
            $record[$k]['datestarttime'] = date("Y.m.d", $v['starttime']);
            $record[$k]['dateendtime'] = date("Y.m.d", $v['endtime']);
            $cha = $v['endtime'] - $v['starttime'];
            $record[$k]['length'] = getSeconds($cha);
        }
        return $record;
    }

    /* 个人主页 */
    public function getUserHome($uid, $touid)
    {
        $info = getUserInfo($touid);

        $info['follows'] = (string)getFollows($touid);
        $info['fans'] = (string)getFans($touid);
        $info['isattention'] = (string)isAttention($uid, $touid);
        $info['isblack'] = (string)isBlack($uid, $touid);
        $info['isblack2'] = (string)isBlack($touid, $uid);

        /* 贡献榜前三 */
        $rs = array();
        $rs = DI()->notorm->users_coinrecord
            ->select("uid,sum(totalcoin) as total")
            ->where('action="sendgift" and touid=?', $touid)
            ->group("uid")
            ->order("total desc")
            ->limit(0, 3)
            ->fetchAll();
        foreach ($rs as $k => $v) {
            $userinfo = getUserInfo($v['uid']);
            $rs[$k]['avatar'] = $userinfo['avatar'];
        }
        $info['contribute'] = $rs;

        /* 视频数 */

        if ($uid == $touid) {  //自己的视频（需要返回视频的状态前台显示）
            $where = " uid={$uid} and isdel='0' and status=1  and is_ad=0";
        } else {  //访问其他人的主页视频
            $videoids_s = getVideoBlack($uid);
            $where = "id not in ({$videoids_s}) and uid={$touid} and isdel='0' and status=1  and is_ad=0";
        }

        $videonums = DI()->notorm->users_video
            ->where($where)
            ->count();
        if (!$videonums) {
            $videonums = 0;
        }

        $info['videonums'] = (string)$videonums;
        /* 直播数 */
        $livenums = DI()->notorm->users_liverecord
            ->where('uid=?', $touid)
            ->count();

        $info['livenums'] = $livenums;
        /* 直播记录 */
        $record = array();
        $record = DI()->notorm->users_liverecord
            ->select("id,uid,nums,starttime,endtime,title,city")
            ->where('uid=?', $touid)
            ->order("id desc")
            ->limit(0, 20)
            ->fetchAll();
        foreach ($record as $k => $v) {
            $record[$k]['datestarttime'] = date("Y.m.d", $v['starttime']);
            $record[$k]['dateendtime'] = date("Y.m.d", $v['endtime']);
            $cha = $v['endtime'] - $v['starttime'];
            $record[$k]['length'] = getSeconds($cha);
        }
        $info['liverecord'] = $record;
        return $info;
    }

    /* 贡献榜 */
    public function getContributeList($touid, $p)
    {
        if ($p < 1) {
            $p = 1;
        }
        $pnum = 50;
        $start = ($p - 1) * $pnum;

        $rs = array();
        $rs = DI()->notorm->users_coinrecord
            ->select("uid,sum(totalcoin) as total")
            ->where('touid=?', $touid)
            ->group("uid")
            ->order("total desc")
            ->limit($start, $pnum)
            ->fetchAll();

        foreach ($rs as $k => $v) {
            $rs[$k]['userinfo'] = getUserInfo($v['uid']);
        }

        return $rs;
    }

    /* 设置分销 */
    public function setDistribut($uid, $code)
    {

        $isexist = DI()->notorm->users_agent
            ->select("*")
            ->where('uid=?', $uid)
            ->fetchOne();
        if ($isexist) {
            return 1004;
        }

        $oneinfo = DI()->notorm->users_agent_code
            ->select("uid")
            ->where('code=? and uid!=?', $code, $uid)
            ->fetchOne();
        if (!$oneinfo) {
            return 1002;
        }

        $agentinfo = DI()->notorm->users_agent
            ->select("*")
            ->where('uid=?', $oneinfo['uid'])
            ->fetchOne();
        if (!$agentinfo) {
            $agentinfo = array(
                'uid' => $oneinfo['uid'],
                'one_uid' => 0,
                'two_uid' => 0,
            );
        }
        /* 判断对方是否自己下级 */
        if ($agentinfo['one_uid'] == $uid || $agentinfo['two_uid'] == $uid) {
            return 1003;
        }

        $data = array(
            'uid' => $uid,
            'one_uid' => $agentinfo['uid'],
            'two_uid' => $agentinfo['one_uid'],
            'three_uid' => $agentinfo['two_uid'],
            'addtime' => time(),
        );
        DI()->notorm->users_agent->insert($data);
        return 0;
    }


    /* 印象标签 */
    public function getImpressionLabel()
    {

        $key = "getImpressionLabel";
        $list = getcaches($key);
        if (!$list) {
            $list = DI()->notorm->impression_label
                ->select("*")
                ->order("orderno asc,id desc")
                ->fetchAll();
            foreach ($list as $k => $v) {
                $list[$k]['colour'] = '#' . $v['colour'];
            }

            setcaches($key, $list);
        }

        return $list;
    }

    /* 用户标签 */
    public function getUserLabel($uid, $touid)
    {
        $list = DI()->notorm->users_label
            ->select("label")
            ->where('uid=? and touid=?', $uid, $touid)
            ->fetchOne();

        return $list;

    }

    /* 设置用户标签 */
    public function setUserLabel($uid, $touid, $labels)
    {
        $nowtime = time();
        $isexist = DI()->notorm->users_label
            ->select("*")
            ->where('uid=? and touid=?', $uid, $touid)
            ->fetchOne();
        if ($isexist) {
            $rs = DI()->notorm->users_label
                ->where('uid=? and touid=?', $uid, $touid)
                ->update(array('label' => $labels, 'uptime' => $nowtime));

        } else {
            $data = array(
                'uid' => $uid,
                'touid' => $touid,
                'label' => $labels,
                'addtime' => $nowtime,
                'uptime' => $nowtime,
            );
            $rs = DI()->notorm->users_label->insert($data);
        }

        return $rs;

    }

    /* 获取我的标签 */
    public function getMyLabel($uid)
    {
        $rs = array();
        $list = DI()->notorm->users_label
            ->select("label")
            ->where('touid=?', $uid)
            ->fetchAll();
        $label = array();
        foreach ($list as $k => $v) {
            $v_a = preg_split('/,|，/', $v['label']);
            $v_a = array_filter($v_a);
            if ($v_a) {
                $label = array_merge($label, $v_a);
            }

        }

        if (!$label) {
            return $rs;
        }


        $label_nums = array_count_values($label);

        $label_key = array_keys($label_nums);

        $labels = $this->getImpressionLabel();

        $order_nums = array();
        foreach ($labels as $k => $v) {
            if (in_array($v['id'], $label_key)) {
                $v['nums'] = (string)$label_nums[$v['id']];
                $order_nums[] = $v['nums'];
                $rs[] = $v;
            }
        }

        array_multisort($order_nums, SORT_DESC, $rs);

        return $rs;

    }

    /* 获取关于我们列表 */
    public function getPerSetting()
    {
        $rs = array();

        $list = DI()->notorm->posts
            ->select("id,post_title")
            ->where("type='2'")
            ->order('orderno asc')
            ->fetchAll();
        foreach ($list as $k => $v) {

            $rs[] = array('id' => '0', 'name' => $v['post_title'], 'thumb' => '', 'href' => get_upload_path("/index.php?g=portal&m=page&a=index&id={$v['id']}"));
        }

        return $rs;
    }

    /* 提现账号列表 */
    public function getUserAccountList($uid)
    {

        $list = DI()->notorm->users_cash_account
            ->select("*")
            ->where('uid=?', $uid)
            ->order("addtime desc")
            ->fetchAll();

        return $list;
    }

    /* 设置提账号 */
    public function setUserAccount($data)
    {

        $rs = DI()->notorm->users_cash_account
            ->insert($data);

        return $rs;
    }

    /* 删除提账号 */
    public function delUserAccount($data)
    {

        $rs = DI()->notorm->users_cash_account
            ->where($data)
            ->delete();

        return $rs;
    }

    /* 登录奖励信息 */
    public function LoginBonus($uid)
    {
        $rs = array(
            'bonus_switch' => '0',
            'bonus_day' => '0',
            'count_day' => '0',
            'bonus_list' => array(),
        );

        //file_put_contents(API_ROOT.'/Runtime/LoginBonus_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').' 提交参数信息 uid:'.json_encode($uid)."\r\n",FILE_APPEND);
        $configpri = getConfigPri();
        if (!$configpri['bonus_switch']) {
            return $rs;
        }
        $rs['bonus_switch'] = $configpri['bonus_switch'];

        //file_put_contents(API_ROOT.'/Runtime/LoginBonus_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').' 提交参数信息 bonus_switch:'."\r\n",FILE_APPEND);
        /* 获取登录设置 */
        $key = 'loginbonus';
        $list = getcaches($key);
        if (!$list) {
            $list = DI()->notorm->loginbonus
                ->select("day,coin")
                ->fetchAll();
            if ($list) {
                setcaches($key, $list);
            }
        }

        //file_put_contents(API_ROOT.'/Runtime/LoginBonus_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').' 提交参数信息 list:'."\r\n",FILE_APPEND);
        $rs['bonus_list'] = $list;
        $bonus_coin = array();
        foreach ($list as $k => $v) {
            $bonus_coin[$v['day']] = $v['coin'];
        }

        /* 登录奖励 */
        $signinfo = DI()->notorm->users_sign
            ->select("bonus_day,bonus_time,count_day")
            ->where('uid=?', $uid)
            ->fetchOne();
        //file_put_contents(API_ROOT.'/Runtime/LoginBonus_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').' 提交参数信息 signinfo:'."\r\n",FILE_APPEND);
        if (!$signinfo) {
            $signinfo = array(
                'bonus_day' => '0',
                'bonus_time' => '0',
                'count_day' => '0',
            );
        }
        $nowtime = time();
        if ($nowtime - $signinfo['bonus_time'] > 60 * 60 * 24) {
            $signinfo['count_day'] = 0;
        }
        $rs['count_day'] = (string)$signinfo['count_day'];

        if ($nowtime > $signinfo['bonus_time']) {
            //更新
            $bonus_time = strtotime(date("Ymd", $nowtime)) + 60 * 60 * 24;
            $bonus_day = $signinfo['bonus_day'];
            if ($bonus_day > 6) {
                $bonus_day = 0;
            }
            $bonus_day++;
            $coin = $bonus_coin[$bonus_day];

            if ($coin) {
                $rs['bonus_day'] = (string)$bonus_day;
            }

        }
        //file_put_contents(API_ROOT.'/Runtime/LoginBonus_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').' 提交参数信息 rs:'."\r\n",FILE_APPEND);
        return $rs;
    }

    /* 获取登录奖励 */
    public function getLoginBonus($uid)
    {
        $rs = 0;
        $configpri = getConfigPri();
        if (!$configpri['bonus_switch']) {
            return $rs;
        }

        /* 获取登录设置 */
        $key = 'loginbonus';
        $list = getcaches($key);
        if (!$list) {
            $list = DI()->notorm->loginbonus
                ->select("day,coin")
                ->fetchAll();
            if ($list) {
                setcaches($key, $list);
            }
        }

        $bonus_coin = array();
        foreach ($list as $k => $v) {
            $bonus_coin[$v['day']] = $v['coin'];
        }

        $isadd = 0;
        /* 登录奖励 */
        $signinfo = DI()->notorm->users_sign
            ->select("bonus_day,bonus_time,count_day")
            ->where('uid=?', $uid)
            ->fetchOne();
        if (!$signinfo) {
            $isadd = 1;
            $signinfo = array(
                'bonus_day' => '0',
                'bonus_time' => '0',
                'count_day' => '0',
            );
        }
        $nowtime = time();
        if ($nowtime > $signinfo['bonus_time']) {
            //更新
            $bonus_time = strtotime(date("Ymd", $nowtime)) + 60 * 60 * 24;
            $bonus_day = $signinfo['bonus_day'];
            $count_day = $signinfo['count_day'];
            if ($bonus_day > 6) {
                $bonus_day = 0;
            }
            if ($nowtime - $signinfo['bonus_time'] > 60 * 60 * 24) {
                $count_day = 0;
            }
            $bonus_day++;
            $count_day++;


            if ($isadd) {
                DI()->notorm->users_sign
                    ->insert(array("uid" => $uid, "bonus_time" => $bonus_time, "bonus_day" => $bonus_day, "count_day" => $count_day));
            } else {
                DI()->notorm->users_sign
                    ->where('uid=?', $uid)
                    ->update(array("bonus_time" => $bonus_time, "bonus_day" => $bonus_day, "count_day" => $count_day));
            }

            $coin = $bonus_coin[$bonus_day];

            if ($coin) {
                DI()->notorm->users
                    ->where('id=?', $uid)
                    ->update(array("coin" => new NotORM_Literal("coin + {$coin}")));


                /* 记录 */
                $insert = array("type" => 'income', "action" => 'loginbonus', "uid" => $uid, "touid" => $uid, "giftid" => $bonus_day, "giftcount" => '0', "totalcoin" => $coin, "showid" => '0', "addtime" => $nowtime);
                DI()->notorm->users_coinrecord->insert($insert);
            }
            $rs = 1;
        }

        return $rs;

    }
}
